//
//  FR_HistoryRecordCell.m
//  FaceRecognition
//
//  Created by Sylar on 2016/11/30.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "FR_HistoryRecordCell.h"
#import "SDPhotoBrowser.h"


@interface FR_HistoryRecordCell()<SDPhotoBrowserDelegate>

@end

@implementation FR_HistoryRecordCell
{
    UILabel *nameLabel;
    UIImageView *headImage;
    UILabel *dateLabel;
    UILabel *classLabel;
    
    NSString *imgURL;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self) {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        
        headImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 9, 40, 40)];
        
        [self.contentView addSubview:headImage];
        
        nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(headImage.right+60, 20, 100, 18)];
        if (SCREENWIDTH < 375) {
            nameLabel.x = headImage.right + 10;
        }
        [nameLabel setFont:[UIFont systemFontOfSize:18]];
        [nameLabel setTextColor:colorFromHex(@"#3b3b3b")];
        [self.contentView addSubview:nameLabel];
        
        classLabel = [[UILabel alloc]initWithFrame:CGRectMake(nameLabel.x, nameLabel.bottom-4, 100, 14)];
        [classLabel setFont:[UIFont systemFontOfSize:14]];
        [classLabel setTextColor:colorFromHex(@"#3b3b3b")];
        classLabel.hidden = YES;
        
        [self.contentView addSubview:classLabel];
        
        dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH - 200, 22, 185, 14)];
        [dateLabel setFont:[UIFont systemFontOfSize:14]];
        dateLabel.textAlignment = NSTextAlignmentRight;
        [dateLabel setTextColor:colorFromHex(@"#979797")];
        [self.contentView addSubview:dateLabel];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 57.5, SCREENWIDTH, 0.5)];
        [lineView setBackgroundColor:colorFromHex(@"#DEDEDE")];
        [self.contentView addSubview:lineView];
    }
    
    return self;
}

- (void)creatCell:(FR_HistoryRecord_Model *)model object:(NSInteger)type {
    
    
    if (type == 1) {
        [nameLabel setText:model.username];
        
        if ([SYAccountTool ShareAccount].account.lasttype == 2) {
            [nameLabel setY:10];
            [nameLabel setFont:[UIFont systemFontOfSize:16]];
            [nameLabel setText:model.screen_camera_position];
            
            classLabel.text = model.classname;
            classLabel.hidden = NO;
        }
        
    }else if (type == 2) {
        [nameLabel setText:model.screen_camera_position];
        
    }
    
    [headImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.photo]] placeholderImage:nil];
    headImage.userInteractionEnabled = YES;
    
    imgURL = model.photo;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [headImage addGestureRecognizer:tap];
    
    if (model.zptime.length == 19) {
        NSString *dateStr = [model.zptime substringToIndex:model.zptime.length-3];
        [dateLabel setText:dateStr];
    }
    
}

-(void)tapAction:(UIGestureRecognizer *)tap{
    
   SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
   browser.sourceImagesContainerView = self.contentView;
   browser.imageCount = 1;
   browser.typeStr = @"jilu";
   browser.currentImageIndex = 0;
   browser.delegate = self;
   [browser show];
}

#pragma mark - photobrowser代理方法

// 返回临时占位图片（即原来的小图）
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index flag:(NSString *)flagStr{
   
   UIImageView *imageView = [[UIImageView alloc] init];
   
   __block UIImage *_image;
   [imageView sd_setImageWithURL:[NSURL URLWithString:imgURL] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
      
      _image = image;
   }];
   return _image;
}


// 返回高质量图片的url
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
   //    NSString *urlStr = [[self.photoItemArray[index] thumbnail_pic] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
   return [NSURL URLWithString:imgURL];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
